define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, $rootScope, T_login, GoPage, Http, GoDialog, Auth, $stateParams, GoPush, GoWapAuth, $timeout, $interval, $ionicSlideBoxDelegate) {

		$scope.$on('$ionicView.afterEnter', function() {
			$scope.updateVerifycode();
			$scope.getthirddata();
		}, false);

		//接收信息之后
		$scope.input = new Object();
		$scope.input.thirdid = $stateParams.uuid;
		$scope.input.thirdtype = $stateParams.type;
		$scope.needVerifyCode = $stateParams.needVerifyCode;
		if($scope.needVerifyCode == "false"){
			$scope.needVerifyCode = false
		}else{
			$scope.needVerifyCode = true;//是否需要输入图片验证码
		}
		$scope.input.headimgurl = "";
		$scope.input.nickname = "";
		$scope.input.username = "";
		$scope.input.password = "";

		$scope.input.cellphonevcode = "";
		$scope.input.mobile = "";
		$scope.input.password1 = "";
		$scope.input.password2 = "";
		//		$scope.input.email = "";
		$scope.input.seller = false; //默认不选

		//去查询用户头像和昵称
		$scope.getthirddata = function() {
			$scope.input.headimgurl = sessionStorage.getItem("auth-headimgurl");
			if($scope.input.headimgurl == undefined)
				$scope.input.headimgurl = "";
			$scope.input.nickname = sessionStorage.getItem("auth-nickname");
			if($scope.input.nickname == undefined)
				$scope.input.nickname = "";
		};
		$scope.showGetRegcode = true;
		$scope.regCodeString = '获取验证码';
		//120s倒计时--张
		$scope.otherString = 's后重新获取';
		$scope.time = "";

		//滑动时候触发
		$scope.selectTab = 0;
		$scope.activeTab = function(index) {
			$scope.selectTab = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
		};
		$scope.slideChanged = function(index) {
			$scope.selectTab = index;
		};
		//禁止左右滑动来切换页面1
		$scope.onTouch = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
		};
		//禁止左右滑动来切换页面2 member.thirdpartlogin
		$scope.onRelease = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(true);
		};
		
		//绑定原有账户
		$scope.onBind = function() {
			//1.绑定失败，保持
			var sourceStr = "";
			if(ionic.Platform.isIOS()) {
				sourceStr = "ios";
			} else {
				sourceStr = "android";
			}

			T_login.thirdpart_bindlogin({
				"unionid": $scope.input.thirdid,
				"type": $scope.input.thirdtype,
				"uname": $scope.input.username,
				"password": $scope.input.password,
				"source": sourceStr
			}, function(res) {
				if(!res.success) {
					GoDialog.toast(res.msg);
					return;
				} else {
					//2.绑定成功，自动登录
					GoDialog.toast('绑定成功');
					//执行用户登录
					$scope.onLogin($scope.input.username, $scope.input.password, function() {
						if(window.cordova && window.cordova.plugins) {
							GoPage.go_back(-2);
						} else {
							window.location.href = HTTP_SHARE_URL + "#/tab/center";
						}
					});
				}
			});
		}
		//账户登录
		$scope.onLogin = function(username, password, callback) {
			var sourceStr = "";
			if(ionic.Platform.isIOS()) {
				sourceStr = "ios";
			} else {
				sourceStr = "android";
			}
			var param = {
				"uname": username,
				"password": password,
				"source": sourceStr
			}
			//登录操作
			T_login.get_login(param, function(res) {
				if(!res.success) {
					GoDialog.alert("提示", res.msg);
					return;
				}
				//极光推送注册
				$timeout(function() {
					GoPush.regist();
				}, 100);

				var info = res.result;
				for(var k in info) {
					if(info[k] == null || info[k] == undefined) {
						info[k] = '';
					}
				}
				//保存用户信息在本地
				Auth.save_loginInfo(info);
				//用户返回到个人中心
				if(callback)
					callback()
			}, function(e) {
				GoDialog.alert("提示", "请求失败，请再试一次");
			}, function() {

			});
		};
		//绑定新账户（注册用户）
		$scope.onBindNew = function() {
			if($scope.onCheck()) {
				var sourceStr = "";
				if(ionic.Platform.isIOS()) {
					sourceStr = "ios";
				} else {
					sourceStr = "android";
				}
				T_login.signup({
					'mobile': $scope.input.mobile,
					'password': $scope.input.password1,
					'nickname': $scope.input.nickname,
					//				'email': $scope.input.email,
					'seller': $scope.input.seller,
					'cellphonevcode': $scope.input.cellphonevcode,
					'thirdid': $scope.input.thirdid,
					'thirdtype': $scope.input.thirdtype,
					"source": sourceStr
				}, function(res) {
					if(!res.success) {
						GoDialog.toast(res.msg);
						return;
					}
					GoDialog.toast('绑定新用户成功');
					//执行用户登录
					$scope.onLogin(res.result.uname, $scope.input.password1, function() {
						//是否进行下一步
						if($scope.input.seller) { //如果是卖家，则跳转卖家资料申请
							GoPage.go_states_back("t-store-data", {}, -2);
						} else { //返回到注册页面
							GoPage.go_back(-2);
						}
					});
				}, function() {
					GoDialog.alert('错误', "系统异常，请重新再试");
				}, function() {

				});
			}
		};
		//提交表单
		$scope.onCheck = function() {
			$scope.input.mobile = ($scope.input.mobile || '').trim();
			$scope.input.cellphonevcode = ($scope.input.cellphonevcode || '').trim();
			$scope.input.nickname = ($scope.input.nickname || '').trim();
			//			$scope.input.email = (input.email || '').trim();
			if(!$scope.input.mobile) {
				GoDialog.alert("输入不正确", "请输入手机号码");
				return false;
			} else if(!$scope.input.cellphonevcode) {
				GoDialog.alert("输入不正确", "请输入手机验证码");
				return false;
			} else if(!$scope.input.password1) {
				GoDialog.alert("输入不正确", "请输入新密码");
				return false;
			} else if(!$scope.input.password2) {
				GoDialog.alert("输入不正确", "请输入确认密码");
				return false;
			} else if($scope.input.password1 != $scope.input.password2) {
				GoDialog.alert("输入不正确", "请确认两次密码输入一致");
				return false;
			}
			return true;
		}

		//获取图形验证码
		$scope.updateVerifycode = function() {
			var session = Auth.get_user_id();
			if(session == "")
				session = Auth.get_session();
			var args = {
				//				"mobile": $scope.input.mobile,
				"ts": new Date().getTime(),
				"method": 'member.verifyCodeMobile',
				"session": session
			};
			//提交数据
			var post_data = {
				sign: Http.sign(args),
				method: args.method,
				api_version: '1.0'
			};

			for(var k in args) {
				post_data[k] = args[k]
			}

			var getparam = '';
			for(var k in post_data) {
				if(!getparam) {
					getparam += '?';
				} else {
					getparam += '&';
				}
				getparam += k + '=' + encodeURIComponent(post_data[k]);
			}
			$scope.input.graphverifycode_url = HTTP_URL + getparam;
		}
		//获取短信验证码
		$scope.onGetRegcode = function() {
			var input = $scope.input;
			if(!input) return;

			input.mobile = (input.mobile || '').trim();
			if(!input.mobile) {
				GoDialog.alert("输入不正确", "请输入手机号码");
				return;
			}

			input.graphverifycode = (input.graphverifycode || '').trim();
			if(!input.graphverifycode&&$scope.needVerifyCode) {
				GoDialog.alert("输入不正确", "请输入图形验证码");
				return;
			}
			getCellphoneCode(input.mobile, input.graphverifycode);
		}
		//检查填写的图形验证码
		function getCellphoneCode(mobile, verifycode) {
			//获取手机验证码
			T_login.get_CellphoneCode({
				'mobile': mobile,
				'verifycode': verifycode,
				"type": 'register'
			}, function(res) {
				console.log(res)
				if(!res.success) {
					GoDialog.alert('注册失败', res.msg);
					return;
				}
				showWaitingRegcode();
			}, function() {
				GoDialog.alert('错误', "系统异常，请重新再试");
			}, function() {

			})
		}
		//等待
		function showWaitingRegcode() {
			$scope.showGetRegcode = false;
			$scope.time = 120;
			var stop = $interval(function() {
				if($scope.time > 0) {
					$scope.time = $scope.time - 1;
				} else {
					$interval.cancel(stop);
					$scope.showGetRegcode = true;
					$scope.time = '';
					$scope.regCodeString = '请重新获取';
				}
			}, 1000);
		}
		//我要申请开店
		$scope.applyStore = function() {
			$scope.input.seller = !$scope.input.seller;
		}
		//密码是否可见
		$scope.inputType = "password";
		$scope.showPassword = function() {
			$scope.inputType = $scope.inputType == "password" ? "text" : "password";
		}
		//密码是否可见
		$scope.inputType1 = "password";
		$scope.showPassword1 = function() {
			$scope.inputType1 = $scope.inputType1 == "password" ? "text" : "password";
		}
		//密码是否可见
		$scope.inputType2 = "password";
		$scope.showPassword2 = function() {
			$scope.inputType2 = $scope.inputType2 == "password" ? "text" : "password";
		}
	}

	controller.$inject = ['$scope', '$rootScope', 'T_login', 'GoPage', 'Http', 'GoDialog', 'Auth', '$stateParams', 'GoPush', 'GoWapAuth', '$timeout', '$interval', '$ionicSlideBoxDelegate'];

	return app.registerController("t_login_authctrl", controller);
});